#
# Makefile

# set here the directories for classes and sources
CLASS_DIR = bin
SOURCE_DIR = src
JARS_DIR = jars

MAIN_CLASS=unibo.lsb.tokyo.Tokyo

SUBDIRS := $(shell find $(SOURCE_DIR) -name "*.java" -printf %h\\n | sort | uniq | sed 's/$(SOURCE_DIR)\/\?//')

IMAGEPATH=/unibo/lsb/res/
IMAGECLASSDIR=$(CLASS_DIR)$(IMAGEPATH)
IMAGEDIR=$(SOURCEDIR)$(IMAGEPATH)

JAVA_HOME=/usr/lib/jvm/java-6-sun-1.6.0.20/bin
#JAVA_HOME=/usr/local/java/bin

# compiler field
#JDEBUGFLAGS = -g -deprecation
#JDEBUGFLAGS = -O -depend -nowarn
JCC = $(JAVA_HOME)/javac
J = $(JAVA_HOME)/java

.PHONY: clean classes compile jar

all:
	mkdir -p $(CLASS_DIR);

	@@for p in $(SUBDIRS); do \
		echo 'compile ' $$p; \
		export SUBDIR=$$p; \
		make --no-print-directory compile; \
    done
    
	@@for i in `cd $(SOURCE_DIR) && find unibo/lsb/res -type d -! -path "*.svn*" -! -name "*svn*"`; do \
		mkdir -p $(CLASS_DIR)/$$i; \
	done

	@@for i in `cd $(SOURCE_DIR) && find unibo/lsb/res -type f -! -path "*.svn*" -name "*.png*" -or -name "*.jpg"`; do \
		cp $(SOURCE_DIR)/$$i $(CLASS_DIR)/$$i; \
	done

clean:
	-rm -f *~ *.class *.bak $(foreach dir,$(SUBDIRS),$(CLASS_DIR)/$(dir)/*.class)

remake:
	clean make

LOCAL_CLASS_DIR=$(CLASS_DIR)/$(SUBDIR)
LOCAL_SOURCE_DIR=$(SOURCE_DIR)/$(SUBDIR)

run:
	$(J) -cp $(CLASS_DIR) $(MAIN_CLASS)

jar:
	cd $(CLASS_DIR); jar -cf ../$(JARS_DIR)/project.jar *

compile: classes

# create target class dir if not present.
$(LOCAL_CLASS_DIR):
	mkdir -p $(LOCAL_CLASS_DIR)

# new rule for java
.SUFFIXES:
.SUFFIXES: .java .class

# magical command that tells make to find class files in another dir
vpath %.class $(LOCAL_CLASS_DIR)
vpath %.java $(LOCAL_SOURCE_DIR)

# classpath; all in ./jars and ./libs
LIBCLASSPATH:=$(CLASS_DIR):$(SOURCE_DIR):$(subst .jar ,.jar:,$(wildcard libs/*.jar libs/*/*.jar libs/*/*/*.jar jars/*.jar))

# compil command
.java.class:
	CLASSPATH=$(LIBCLASSPATH) $(JCC) -nowarn -d $(CLASS_DIR) $(JDEBUGFLAGS) $<

# find out target files
PATHFILES = $(wildcard $(LOCAL_SOURCE_DIR)/*.java)
FILES = $(subst $(LOCAL_SOURCE_DIR)/,,$(PATHFILES))
classes: $(FILES:.java=.class)
